home *** CD-ROM | disk | FTP | other *** search
/ Shareware Super Platinum 8 / Shareware Super Platinum 8.iso / mac / PROGTOOL / GWMALLOC.ZIP;1 / GWMALLOC.TAR / gw_malloc / malloc_t.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-08  |  6.1 KB  |  239 lines

  1. /*
  2.  * test program for malloc code
  3.  *
  4.  * Copyright 1992 by Gray Watson and the Antaire Corporation
  5.  *
  6.  * This file is part of the malloc-debug package.
  7.  *
  8.  * This library is free software; you can redistribute it and/or
  9.  * modify it under the terms of the GNU Library General Public
  10.  * License as published by the Free Software Foundation; either
  11.  * version 2 of the License, or (at your option) any later version.
  12.  *
  13.  * This library is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.  * Library General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU Library General Public
  19.  * License along with this library (see COPYING-LIB); if not, write to the
  20.  * Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  *
  22.  * The author of the program may be contacted at gray.watson@antaire.com
  23.  */
  24.  
  25. /*
  26.  * Test program for the malloc library.  Current it is interactive although
  27.  * probably should be script based.
  28.  */
  29.  
  30. #include <stdio.h>                /* for stdin */
  31.  
  32. #include "malloc.h"
  33.  
  34. #if INCLUDE_RCS_IDS
  35. LOCAL    char    *rcs_id =
  36.   "$Id: malloc_t.c,v 1.15 1993/04/06 04:24:44 gray Exp $";
  37. #endif
  38.  
  39. #define DEFAULT_ITERATIONS    1000
  40.  
  41. /*
  42.  * hexadecimal STR to integer translation
  43.  */
  44. LOCAL    int    hex_to_int(char * str)
  45. {
  46.   int        ret;
  47.   
  48.   /* strip off spaces */
  49.   for (; *str == ' ' || *str == '\t'; str++);
  50.   
  51.   /* skip a leading 0[xX] */
  52.   if (*str == '0' && (*(str + 1) == 'x' || *(str + 1) == 'X'))
  53.     str += 2;
  54.   
  55.   for (ret = 0;; str++) {
  56.     if (*str >= '0' && *str <= '9')
  57.       ret = ret * 16 + (*str - '0');
  58.     else if (*str >= 'a' && *str <= 'f')
  59.       ret = ret * 16 + (*str - 'a' + 10);
  60.     else if (*str >= 'A' && *str <= 'F')
  61.       ret = ret * 16 + (*str - 'A' + 10);
  62.     else
  63.       break;
  64.   }
  65.   
  66.   return ret;
  67. }
  68.  
  69. /*
  70.  * read an address from the user
  71.  */
  72. LOCAL    long    get_address(void)
  73. {
  74.   char    line[80];
  75.   long    pnt;
  76.   
  77.   do {
  78.     (void)printf("Enter a hex address: ");
  79.     (void)fgets(line, sizeof(line), stdin);
  80.   } while (line[0] == '\0');
  81.   
  82.   pnt = hex_to_int(line);
  83.   
  84.   return pnt;
  85. }
  86.  
  87. EXPORT    int    main(int argc, char ** argv)
  88. {
  89.   argc--, argv++;
  90.   
  91.   (void)srand(time(0) ^ 0xDEADBEEF);
  92.   
  93.   (void)printf("------------------------------------------------------\n");
  94.   (void)printf("Malloc test program.  Type 'help' for assistance.\n");
  95.   
  96.   for (;;) {
  97.     int        len;
  98.     char    line[128], *linep;
  99.     
  100.     (void)printf("------------------------------------------------------\n");
  101.     (void)printf("prompt> ");
  102.     (void)fgets(line, sizeof(line), stdin);
  103.     linep = (char *)index(line, '\n');
  104.     if (linep != NULL)
  105.       *linep = '\0';
  106.     
  107.     len = strlen(line);
  108.     if (len == 0)
  109.       continue;
  110.     
  111.     if (strncmp(line, "?", len) == 0
  112.     || strncmp(line, "help", len) == 0) {
  113.       (void)printf("------------------------------------------------------\n");
  114.       (void)printf("HELP:\n\n");
  115.       
  116.       (void)printf("help      - print this message\n\n");
  117.       
  118.       (void)printf("malloc    - allocate memory\n");
  119.       (void)printf("free      - deallocate memory\n");
  120.       (void)printf("realloc   - reallocate memory\n\n");
  121.       
  122.       (void)printf("map       - map the heap to the logfile\n");
  123.       (void)printf("overwrite - overwrite some memory to test errors\n");
  124.       (void)printf("random    - randomly execute a number of malloc/frees\n");
  125.       (void)printf("verify    - check out a memory address\n\n");
  126.       
  127.       (void)printf("quit      - quit this test program\n");
  128.       continue;
  129.     }
  130.     
  131.     if (strncmp(line, "quit", len) == 0)
  132.       break;
  133.     
  134.     if (strncmp(line, "malloc", len) == 0) {
  135.       int    size;
  136.       
  137.       (void)printf("How much: ");
  138.       (void)fgets(line, sizeof(line), stdin);
  139.       size = atoi(line);
  140.       (void)printf("malloc(%d) returned: %#lx\n", size, (long)MALLOC(size));
  141.       continue;
  142.     }
  143.     
  144.     if (strncmp(line, "free", len) == 0) {
  145.       long    pnt;
  146.       
  147.       pnt = get_address();
  148.       (void)printf("free(%#lx) returned: %s\n",
  149.            pnt, (FREE(pnt) == FREE_NOERROR ? "success" : "failure"));
  150.       continue;
  151.     }
  152.     
  153.     if (strncmp(line, "realloc", len) == 0) {
  154.       int    size;
  155.       long    pnt;
  156.       
  157.       pnt = get_address();
  158.       
  159.       (void)printf("How much: ");
  160.       (void)fgets(line, sizeof(line), stdin);
  161.       size = atoi(line);
  162.       
  163.       (void)printf("realloc(%#lx, %d) returned: %#lx\n",
  164.            pnt, size, (long)REMALLOC(pnt, size));
  165.       
  166.       continue;
  167.     }
  168.     
  169.     if (strncmp(line, "map", len) == 0) {
  170.       (void)malloc_heap_map();
  171.       (void)printf("Done.\n");
  172.       continue;
  173.     }
  174.     
  175.     if (strncmp(line, "overwrite", len) == 0) {
  176.       long    pnt, magic;
  177.       
  178.       pnt = get_address();
  179.       
  180.       magic = 0x12345678;
  181.       bcopy(&magic, (char *)pnt, sizeof(magic));
  182.       
  183.       (void)printf("Done.\n");
  184.       continue;
  185.     }
  186.     
  187.     /* do random heap hits */
  188.     if (strncmp(line, "random", len) == 0) {
  189.       int    count, max;
  190.       
  191.       (void)printf("How many iterations[%d]: ", DEFAULT_ITERATIONS);
  192.       (void)fgets(line, sizeof(line), stdin);
  193.       if (line[0] == '\0' || line[0] == '\n')
  194.     max = DEFAULT_ITERATIONS;
  195.       else
  196.     max = atoi(line);
  197.       
  198.       for (count = 1; count < max; count += 10) {
  199.     int    amount;
  200.     char    *data;
  201.     
  202.     amount = rand() % (count * 10) + 1;
  203.     data = MALLOC(amount);
  204.     (void)FREE(data);
  205.       }
  206.       
  207.       (void)printf("Done.\n");
  208.       continue;
  209.     }
  210.     
  211.     if (strncmp(line, "verify", len) == 0) {
  212.       long    pnt;
  213.       int    ret;
  214.       
  215.       pnt = get_address();
  216.       
  217.       ret = malloc_verify((char *)pnt);
  218.       (void)printf("malloc_verify(%#lx) returned: %s\n",
  219.            pnt,
  220.            (ret == MALLOC_VERIFY_NOERROR ? "success" : "failure"));
  221.       continue;
  222.     }
  223.     
  224.     (void)printf("Unknown command '%s'.\n", line);
  225.     (void)printf("Type 'help' for assistance.\n");
  226.   }
  227.   
  228.   /* shutdown the alloc routines */
  229.   malloc_shutdown();
  230.   
  231.   (void)printf("------------------------------------------------------\n");
  232.   (void)printf("final malloc_verify returned: %s\n",
  233.            (malloc_verify(NULL) == MALLOC_VERIFY_NOERROR ? "success" :
  234.         "failure"));
  235.   (void)printf("------------------------------------------------------\n");
  236.   
  237.   exit(0);
  238. }
  239.